package com.topvision.camerasdk.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.hardware.Camera;
import android.net.Uri;
import android.text.TextUtils;

import com.topvision.commomsdk.table.CameraTable;
import com.topvision.commomsdk.uitl.TvDeviceUtil;


/**
 * 照相机和录制视屏通用的类,该类主要用于获取系统设置中camera的色度、对比度、饱和度的值.
 * User: jack(jackgu@topvision-cv.com)
 * Date: 2017-01-05
 * Time: 17:03
 */
public class CameraSettingsUtil {
    /**
     * 从系统设置中获取camera亮度、对比度、饱和度的一些参数
     * create at 2017/1/5 16:12
     */
    public static Camera.Parameters geFromSettings(Context context, Camera.Parameters parameters) {
        Uri uri = Uri.parse("content://com.topvision.providers.settings.cameraProvider/camera");
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(uri, null, null, null, "id asc");
        if (cursor != null && cursor.moveToNext()) {
            //亮度
            String brightness = cursor.getString(cursor
                    .getColumnIndex(CameraTable.COLUMN_BRIGHTNESS));
            if (!TextUtils.isEmpty(brightness)) {
                parameters.set(CameraTable.COLUMN_BRIGHTNESS, brightness);
            } else {
                parameters.set(CameraTable.COLUMN_BRIGHTNESS, 75);
            }
            //饱和度
            String saturation = cursor.getString(cursor
                    .getColumnIndex(CameraTable.COLUMN_SATURATION));
            if (!TextUtils.isEmpty(saturation)) {
                parameters.set(CameraTable.COLUMN_SATURATION, saturation);
            } else {
                parameters.set(CameraTable.COLUMN_SATURATION, 45);
            }
            //对比度
            String contrast = cursor.getString(cursor
                    .getColumnIndex(CameraTable.COLUMN_CONTRAST));
            if (!TextUtils.isEmpty(contrast)) {
                parameters.set(CameraTable.COLUMN_CONTRAST, contrast);
            } else {
                parameters.set(CameraTable.COLUMN_CONTRAST, 55);
            }
            //白平衡
            String whiteBalance = cursor.getString(cursor
                    .getColumnIndex(CameraTable.COLUMN_WHITE_BALANCE));
            if (!TextUtils.isEmpty(whiteBalance)) {
                parameters.setWhiteBalance(whiteBalance);
            } else {
                parameters.setWhiteBalance("auto");
            }
            //颜色效果
            String colorEffect = cursor.getString(cursor
                    .getColumnIndex(CameraTable.COLUMN_COLOR_EFFECT));
            if (!TextUtils.isEmpty(colorEffect)) {
                parameters.setColorEffect(colorEffect);
            } else {
                parameters.setColorEffect("natural");
            }
            cursor.close();
        } else {
            if (TvDeviceUtil.isJGMP()) {
                //初始值
                parameters.set(CameraTable.COLUMN_BRIGHTNESS, 75);
                parameters.set(CameraTable.COLUMN_SATURATION, 45);
                parameters.set(CameraTable.COLUMN_CONTRAST, 55);
                parameters.setWhiteBalance("auto");
                parameters.setColorEffect("natural");
            }

        }
        return parameters;
    }
}